/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.CuaHangDAO;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Logger;
import pojo.CuaHang;

/**
 *
 * @author softviet
 */
public class DanhSachCuaHangGUI extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachCuaHang
     */
    private List<CuaHang> cuaHangs;
    private static Logger logger = Logger.getLogger(DanhSachCuaHangGUI.class);
    private JErrorProvider errorProvider;
    
    public DanhSachCuaHangGUI() throws Exception {
        initComponents();
        errorProvider=new JErrorProvider();
        FormatControl();
        AutoLoad();
    }
    
    private void FormatControl(){
        Utility.FormatNumberStringTextfield(txtDienThoai);
    }
    
    private void AutoLoad() throws Exception{
        cuaHangs=(List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class, MainGUI.nguoiDung);
        Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhSachCuaHang, new String[]{"Mã cửa hàng","Tên cửa hàng","Địa chỉ","Điện thoại"}, null);
        tbDanhSachCuaHang.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                tbDanhSachCuaHangMouseClicked(null);
            }
        });
    }
    
    private int CheckDataBeforeThem(){
        int dem=0;
        errorProvider.clear();
        if(txtTenCuaHang.getText().trim().isEmpty()){
            errorProvider.setError(txtTenCuaHang, "required");
            dem++;
        }
        if(txtDiaChi.getText().trim().isEmpty()){
            errorProvider.setError(txtDiaChi, "required");
            dem++;
        }
        if(txtDienThoai.getText().trim().isEmpty()){
            errorProvider.setError(txtDienThoai, "required");
            dem++;
        }
        return dem;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtTenCuaHang = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDienThoai = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachCuaHang = new org.jdesktop.swingx.JXTable();

        jLabel1.setText("Tên cửa hàng");

        jLabel2.setText("Địa chỉ");

        jLabel3.setText("Điện thoại");

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setText("Xoá");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        tbDanhSachCuaHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachCuaHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachCuaHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachCuaHang);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTenCuaHang, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .addComponent(txtDiaChi)
                            .addComponent(txtDienThoai)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(219, 219, 219)
                        .addComponent(btnThem)
                        .addGap(18, 18, 18)
                        .addComponent(btnSua)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa)))
                .addContainerGap(241, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTenCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        try {
            // TODO add your handling code here:
            if(CheckDataBeforeThem()>0){
                return;
            }
            CuaHang cuaHang=new CuaHang(0, txtTenCuaHang.getText().trim(), txtDiaChi.getText().trim(), txtDienThoai.getText(), false, null, null, null, null, null);
            int id=CuaHangDAO.Them(cuaHang);
            cuaHang.setId(id);
            cuaHangs.add(cuaHang);
            Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhSachCuaHang, new String[]{"Mã cửa hàng","Tên cửa hàng","Địa chỉ","Điện thoại"}, null);
            tbDanhSachCuaHang.changeSelection(tbDanhSachCuaHang.getModel().getRowCount()-1, 0, false, false);
            JOptionPane.showMessageDialog(null, "Thêm thành công");
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        List<CuaHang> chs=(List<CuaHang>) Utility.GetSelectedRows(tbDanhSachCuaHang, cuaHangs);
        if(!chs.isEmpty()){
            if(JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xoá không?","Question",JOptionPane.YES_NO_OPTION)==0){
                try {
                    for(CuaHang cuaHang:chs){
                        cuaHang.setIsDelete(true);
                    }
                    CuaHangDAO.Xoa(chs);
                    Utility.RemoveSelectedRows(CuaHang.class, tbDanhSachCuaHang, cuaHangs, new String[]{"Mã cửa hàng","Tên cửa hàng","Địa chỉ","Điện thoại"}, null);
                    JOptionPane.showMessageDialog(this, "Xoá thành công");
                    Utility.ClearControlInPanel(this);
                } catch (Exception ex) {
                    logger.error(new Date().toString(), ex);
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để xoá");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        if(CheckDataBeforeThem()>0){
                return;
        }
        List<CuaHang> chs=(List<CuaHang>) Utility.GetSelectedRows(tbDanhSachCuaHang, cuaHangs);
        if(!chs.isEmpty()){
            try {
                int index=cuaHangs.indexOf(chs.get(0));
                chs.get(0).setDiaChi(txtDiaChi.getText().trim());
                chs.get(0).setDienThoai(txtDienThoai.getText().trim());
                CuaHangDAO.Sua(chs.get(0));
                Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhSachCuaHang, new String[]{"Mã cửa hàng","Tên cửa hàng","Địa chỉ","Điện thoại"}, null);
                tbDanhSachCuaHang.changeSelection(index, 0, false, false);
                JOptionPane.showMessageDialog(this, "Cập nhật thành công");
            } catch (Exception ex) {
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để sửa");
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void tbDanhSachCuaHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachCuaHangMouseClicked
        // TODO add your handling code here:
        List<CuaHang> chs=(List<CuaHang>) Utility.GetSelectedRows(tbDanhSachCuaHang, cuaHangs);
        if(!chs.isEmpty()){
            txtTenCuaHang.setText(chs.get(0).getTenCuaHang());
            txtDiaChi.setText(chs.get(0).getDiaChi());
            txtDienThoai.setText(chs.get(0).getDienThoai());
        }
    }//GEN-LAST:event_tbDanhSachCuaHangMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbDanhSachCuaHang;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtTenCuaHang;
    // End of variables declaration//GEN-END:variables
}
